package com.yayan.xh.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yayan.util.RequestUtil;
import com.yayan.xh.dao.IMemberDao;
import com.yayan.xh.dao.IShopDao;
import com.yayan.xh.dao.MemberDao;
import com.yayan.xh.dao.ShopDao;

@Service
public class MemberService implements IMemberService {
	
	private IShopDao shopDao;
	
	private IMemberDao memberDao;

	public MemberService() {
		shopDao = new ShopDao();
		memberDao = new MemberDao();
	}
	
	@Override
	public Map<String, Object> frontLogin(Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> shopMap = shopDao.getShopByShopId(map);
			String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + shopMap.get("s_appid").toString() 
					+ "&secret="+ shopMap.get("s_secret") + "&js_code=" + map.get("code") + "&grant_type=authorization_code";
			String jsonStr = RequestUtil.httpsRequest(requestUrl, "POST", null);
			
			JSONObject obj = JSONObject.parseObject(jsonStr);			
			
			if(obj.getString("errcode") != null && !obj.getString("errcode").isEmpty()){
				result.put("status", false);
				result.put("errmsg", "code错误！");
				return result;
			}
			map.put("openId", obj.getString("openid"));
			Map<String, Object> userMap = memberDao.getMemberByOpenid(map);
			
			//如果用户信息不存在则将openid存入数据库，并获取用户id，否则直接获取用户id。
			if(userMap == null || userMap.isEmpty()){
				result.put("userId", memberDao.saveMember(map));
				//flag设为0，表示第一次插入该用户，此时可将获取的微信用户信息更新到数据库
				result.put("flag", 0);
			}else{
				result.put("userId", userMap.get("u_id"));
				result.put("flag", 1);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean saveUser(Map<String, String> map) {
		boolean f = false;
		try {
			if(null != map.get("userId") && null != map.get("wxNickName") && null != map.get("wxAvatarUrl") && null != map.get("wxGender")
					&& !"0".equalsIgnoreCase(map.get("userId")) && !"0".equalsIgnoreCase(map.get("wxNickName")) 
					&& !"0".equalsIgnoreCase(map.get("wxAvatarUrl")) && !"0".equalsIgnoreCase(map.get("wxGender"))) {
				f = memberDao.updateMember(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

	@Override
	public Map<String, Object> getUser(Map<String, String> map) {
		Map<String, Object> result = null;
		try {
			if(null != map.get("userId") && !"0".equalsIgnoreCase(map.get("userId")) 
					&& null != map.get("shopId") && !"0".equalsIgnoreCase(map.get("shopId"))) {
				result = memberDao.getMemberByUid(map);
			}
		} catch (Exception e) {
			
		}
		return result;
	}

	@Override
	public boolean modifyUserTabel(Map<String, String> map) {
		boolean f = false;
		try {
			if(null != map.get("userId") && !"0".equalsIgnoreCase(map.get("userId")) 
					&& null != map.get("shopId") && !"0".equalsIgnoreCase(map.get("shopId"))) {
				f = memberDao.modifyUserTable(map);
			}
		} catch (Exception e) {
			
		}
		return f;
	}

	
}
